<include file="public:header" />
<include file="public:left" />
<style>
    #select-area select{height:30px;}
</style>
<div class="page_title">
    <h2 class="fl">编辑奖品</h2>
    <a class="fr" href="{:url('Prizes/index',array('activity'=>$activity_id))}">奖品列表</a>
</div>
<form  method="post" enctype="multipart/form-data" id="brandForm" name="brandForm">
    <section>
        <ul class="ulColumn2">
            <li>
                <span class="item_name" style="width:120px;">奖品名称：</span>
                <input type="text" class="textbox textbox_295" name="prize_name" placeholder="奖品名称" value="{$activity_prizes_find['prize_name']}"/>
            </li>
            <li>
                <span class="item_name" style="width:120px;">奖品图片：</span>
                <div class="textbox textbox_295" style="width:200px;display: inline-block;">
                    <input type="file" name="prizes_image" />
                </div>
                <img src="{$config_update}{$activity_prizes_find['prize_img']}" width="150" height="90">
            </li>
            <if condition="!isset($id)">
                <li>
                    <span class="item_name" style="width:120px;">奖品类型：</span>
                    <!-- <input name="type" type="radio" value="2" checked>实体 -->
                    <input name="type" type="radio" value="1" >中奖
                    <input name="type" type="radio" value="2" >未中奖
                </li>
            <else />
                <li>
                    <span class="item_name" style="width:120px;">奖品状态：</span>
                    <!-- <if condition="$activity_prizes_find['type'] eq '1'">虚拟</if> -->
                    <if condition="$activity_prizes_find['type'] eq '1'">中奖</if>
                    <if condition="$activity_prizes_find['type'] eq '2'">未中奖</if>
                </li>
            </if>

            <li id="prizes_count">
                <span class="item_name" style="width:120px;">中奖概率：</span>
                <input type="number" min="1" class="textbox textbox_295" name="number_prizes"  placeholder="整数" value="{$activity_prizes_find['probability']}"/>%
            </li>

            <li id="prizes_money">
                <span class="item_name" style="width:120px;">红包金额(注：金额一旦填入奖品将以现金发放)：</span>
                <input type="mon" min="1" class="textbox textbox_295" name="prizes_money"  placeholder="整数" value="{$activity_prizes_find['money']}"/>
            </li>

            <li id="prizes_money">
                <span class="item_name" style="width:120px;">奖品库存：</span>
                <input type="mon" min="0" class="textbox textbox_295" name="stock"  placeholder="整数" value="{$activity_prizes_find['stock']}"/>
            </li>

            <li id="prizes_count">
            <span class="item_name" style="width:120px;">奖池概率：</span><br/><br/>
                <volist name="activity_list" id="vo">
                <if condition="$vo.id neq $id">
                <span class="item_name" style="width:150px;">{$vo.prize_name}：</span>  
                    <input style="margin-bottom: 8px" type="number" min="1" class="textbox textbox_295 probability_prizes" name="probability_prizes"  placeholder="" value="{$vo.probability}" disabled="disabled"/>%<br/>
                </if>
                </volist>
            </li>

            <li>
                <span class="item_name" style="width:120px;"></span>
                <input type="button" class="link_btn" value="提交"/>
                <input type="hidden" name="activity_id" value="{$activity_id}">
                <input type="hidden" name="id" value="{$id}">
            </li>
        </ul>
    </section>
</form>
<!--tabStyle-->
<style type="text/css" src="__STATIC__/admin/js/laydate/need/laydate.css" ></style>
<script src="__STATIC__/admin/js/laydate/laydate.js"></script>

<script type="text/javascript">
    $(document).ready(function(){

        $('input[name="type"]').change(function(){
            var type = $(this).val();

            if(type == '2'){
                $('input[name="prize_name"]').val('未中奖，谢谢参与!');
            }else{
                if($('input[name="prize_name"]').val() == '未中奖，谢谢参与!'){
                    $('input[name="prize_name"]').val(null);
                }
            }
        });


        $(".link_btn").click(function(){

            if($("#name").val()==""){
                layer.alert("奖品名称为空!");
                return false;
            }

            var num = $('input[name="number_prizes"]').val();
            if(parseInt(num)==0 || num<0 || num>100){
                layer.alert("中奖概率不能为0少于0大于100");
                return false;
            }

            if(typeof($('input[name="type"]:checked').val())=="undefined" && $('input[name="id"]').val()==""){
                layer.alert("请勾选奖品类型!");
                return false;
            }

            if($('input[name="id"]').val()=="" && $('input[name="number_prizes"]').val()==""){
                layer.alert("请填写奖品数量");
                return false;
            }

            if($('input[name="prizes_money"]').val()=="" && $('input[name="prizes_money"]').val()==""){
                layer.alert("请填写红包金额");
                return false;
            }
            var formData = new FormData();
            var prize_name = $('input[name="prize_name"]').val();
            var type = $('input[name="type"]:checked ').val();
            var number_prizes = $('input[name="number_prizes"]').val();
            var activity_id = $('input[name="activity_id"]').val();
            var id = $('input[name="id"]').val();
            var stock = $('input[name="stock"]').val();
            var prizes_money = $('input[name="prizes_money"]').val();
            var prizes_image = $('input[name="prizes_image"]').get(0).files[0];

            formData.append('prize_name',prize_name);
            if(typeof(type) != 'undefined'){
                formData.append('type',type);
            }
            formData.append('number_prizes',number_prizes);
            formData.append('activity_id',activity_id);
            formData.append('prizes_money',prizes_money);
            formData.append('prizes_image',prizes_image);
            formData.append('stock',stock);
            formData.append('id',id);
           
            $.ajax({
                url:"{:url('Prizes/add')}",
                type:'POST',
                data:formData,
				dataType: 'json',
                async:false,
                processData:false,
                contentType:false,
                success:function(result){
                    if(result.code == '0'){
                        layer.alert(result.msg);
                    }else{
                        layer.alert(result.msg, {
                            btn: ['确定']
                        },
                        function (index, item) {
                            location.href = result.url;
                        });
                    }
                }

            });
        });
    });
</script>
</div>
</section>
</body>
</html>
